﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RFIDStockManagement.Common.Public.Enums;

namespace RFIDStockManagement.Common.Public
{
    public static class SpeedwayPublicFunction
    {
        private static string OutStockSpeedwayConfigPath = AppDomain.CurrentDomain.BaseDirectory + "\\OutStockSpeedway.cfg";
        /// <summary>
        /// 出库reader地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetOutStockSpeedwayCfg()
        {
            string[] result = null;
            if (File.Exists(OutStockSpeedwayConfigPath))
            {
                FileStream fs = new FileStream(OutStockSpeedwayConfigPath, FileMode.Open);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();
                string strData = new UTF8Encoding().GetString(data);
                result = strData.Split('|');
                if (result.Length != 2)
                {
                    result = null;
                }
                else
                {
                    ReadCfgInfo info = new ReadCfgInfo() { IOStockOperate = EnumIOStockOperate.OutStock, ReadName = result[0], PortNumber = new List<int>() };
                    string[] numbers = result[1].Split(',');
                    foreach (string item in numbers)
                    {
                        info.PortNumber.Add(int.Parse(item));
                    }
                    m_ReadNumber[EnumIOStockOperate.OutCheck] = info;

                }
            }

            return result;
        }
        public static void WriteOutStockSpeedwayCfg(string data)
        {
            PublicFunction.WriteFile(OutStockSpeedwayConfigPath, data);
        }
        

        private static string InStockSpeedwayConfigPath = AppDomain.CurrentDomain.BaseDirectory + "\\InStockSpeedway.cfg";
        /// <summary>
        /// 入库reader地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetInStockSpeedwayCfg()
        {
            string[] result = null;
            if (File.Exists(InStockSpeedwayConfigPath))
            {
                FileStream fs = new FileStream(InStockSpeedwayConfigPath, FileMode.Open);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();
                string strData = new UTF8Encoding().GetString(data);
                result = strData.Split('|');
                if (result.Length != 2)
                {
                    result = null;
                }
                else
                {
                    ReadCfgInfo info = new ReadCfgInfo() { IOStockOperate = EnumIOStockOperate.InStock, ReadName = result[0], PortNumber = new List<int>() };
                    string[] numbers = result[1].Split(',');
                    foreach (string item in numbers)
                    {
                        info.PortNumber.Add(int.Parse(item));
                    }
                    m_ReadNumber[EnumIOStockOperate.InStock] = info;

                }
            }

            return result;
        }
        public static void WriteInStockSpeedwayCfg(string data)
        {
            PublicFunction.WriteFile(InStockSpeedwayConfigPath, data);
        }


        private static string OutCheckSpeedwayConfigPath = AppDomain.CurrentDomain.BaseDirectory + "\\OutCheckSpeedway.cfg";
        /// <summary>
        /// 出库复核reader地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetOutCheckSpeedwayCfg()
        {
            string[] result = null;
            if (File.Exists(OutCheckSpeedwayConfigPath))
            {
                FileStream fs = new FileStream(OutCheckSpeedwayConfigPath, FileMode.Open);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();
                string strData = new UTF8Encoding().GetString(data);
                result = strData.Split('|');
                if (result.Length != 2)
                {
                    result = null;
                }
                else
                {
                    ReadCfgInfo info = new ReadCfgInfo() { IOStockOperate = EnumIOStockOperate.OutCheck, ReadName = result[0], PortNumber = new List<int>() };
                    string[] numbers = result[1].Split(',');
                    foreach (string item in numbers)
                    {
                        info.PortNumber.Add(int.Parse(item));
                    }
                    m_ReadNumber[EnumIOStockOperate.OutCheck] = info;

                }
            }

            return result;
        }
        public static void WriteOutCheckSpeedwayCfg(string data)
        {
            PublicFunction.WriteFile(OutCheckSpeedwayConfigPath, data);
        }

        private static string StockCheckSpeedwayConfigPath = AppDomain.CurrentDomain.BaseDirectory + "\\StockCheckSpeedway.cfg";
        /// <summary>
        /// 盘点reader地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetStockCheckSpeedwayCfg()
        {
            string[] result = null;
            if (File.Exists(StockCheckSpeedwayConfigPath))
            {
                FileStream fs = new FileStream(StockCheckSpeedwayConfigPath, FileMode.Open);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();
                string strData = new UTF8Encoding().GetString(data);
                result = strData.Split('|');
                if (result.Length != 2)
                {
                    result = null;
                }
                else
                {
                    ReadCfgInfo info = new ReadCfgInfo() { IOStockOperate = EnumIOStockOperate.StockCheck, ReadName = result[0], PortNumber = new List<int>() };
                    string[] numbers = result[1].Split(',');
                    foreach (string item in numbers)
                    {
                        info.PortNumber.Add(int.Parse(item));
                    }
                    m_ReadNumber[EnumIOStockOperate.StockCheck] = info;

                }
            }

            return result;
        }
        public static void WriteStockCheckSpeedwayCfg(string data)
        {
            PublicFunction.WriteFile(StockCheckSpeedwayConfigPath, data);
        }


        private static Dictionary<EnumIOStockOperate, ReadCfgInfo> m_ReadNumber = new Dictionary<EnumIOStockOperate, ReadCfgInfo>();
        public static EnumIOStockOperate CheckIOStockOperate(string readName, int portNumber)
        {
            EnumIOStockOperate result = EnumIOStockOperate.None;
            foreach (ReadCfgInfo info in m_ReadNumber.Values)
            {
                if (info.ReadName == readName && info.PortNumber.Contains(portNumber))
                {
                    result = info.IOStockOperate;
                    break;
                }
            }

            return result;

        }

        public static string ConvertRfid(long itemId, long unitId, long tagId)
        {
            string strResult = string.Empty;
            string strItemId = String.Format("{0:X" + PublicConst.TagItemBit.ToString() + "}", itemId);
            string strUnitId = String.Format("{0:X" + PublicConst.TagUnitBit.ToString() + "}", unitId);
            string strTagId = String.Format("{0:X" + PublicConst.TagItemGuidBit.ToString() + "}", tagId);

            string rfid = string.Format("{0}{1}{2}{3}",
                PublicConst.TagSysMark, strItemId, strUnitId, strTagId);
            //int iCount = PublicConst.TagMaxBit / 4;
            //for (int i = 0; i < iCount; i++)
            //{
            //    strResult = string.Format("{0}-{1}", strResult, rfid.Substring(i * 4, 4));
            //}
            //return strResult.TrimStart('-');

            return rfid;
        }
        public static long[] ConvertRfid(string rfid, out bool bSuccess)
        {
            long[] arrResult = new long[3];
            bSuccess = false;
            try
            {
                //string rfidTemp = rfid.Replace("-", "");
                if (rfid.Length != PublicConst.TagMaxBit
                    || rfid.Substring(0, 4) != PublicConst.TagSysMark)
                {
                    bSuccess = false;
                    return null;
                }

                int iStart = PublicConst.TagSysMarkBit;
                arrResult[0] = Convert.ToInt64(rfid.Substring(iStart, PublicConst.TagItemBit), 16);
                iStart += PublicConst.TagItemBit;
                arrResult[1] = Convert.ToInt64(rfid.Substring(iStart, PublicConst.TagUnitBit), 16);
                iStart += PublicConst.TagUnitBit;
                arrResult[2] = Convert.ToInt64(rfid.Substring(iStart, PublicConst.TagItemGuidBit), 16);
                bSuccess = true;
            }
            catch (Exception ex)
            {
            }

            return arrResult;
        }
    }

    public class ReadCfgInfo
    {
        public string ReadName { get; set; }
        public EnumIOStockOperate IOStockOperate { get; set; }
        public List<int> PortNumber { get; set; }
    }
}
